/*
  Diamond Girl - Game where player collects diamonds.
  Copyright (C) 2005-2014  Joni Yrjänä <joniyrjana@gmail.com>
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


  Complete license can be found in the LICENSE file.
*/

#include "map.h"

#include <assert.h>
#ifdef WITH_CHECKSUMMING
# include <mhash.h>
#endif

#ifdef WITH_CHECKSUMMING
char * map_calculate_hash(struct map * map)
{
  char * rv;

  rv = NULL;
  assert(map != NULL);


  unsigned char * hash;
  
  hash = malloc(mhash_get_block_size(MHASH_MD5));
  assert(hash != NULL);
  if(hash != NULL)
    {
      MHASH td;

      td = mhash_init(MHASH_MD5);
      if(td != MHASH_FAILED)
        {
          int len;

          /* Hash together all the relevant data. */
          mhash(td, map->cave_name,        strlen(map->cave_name)     );
          mhash(td, &map->level,           sizeof map->level          );
          mhash(td, &map->is_intermission, sizeof map->is_intermission);
          mhash(td, &map->width,           sizeof map->width          );
          mhash(td, &map->height,          sizeof map->height         );
          mhash(td, &map->start_x,         sizeof map->start_x        );
          mhash(td, &map->start_y,         sizeof map->start_y        );
          mhash(td, &map->exit_x,          sizeof map->exit_x         );
          mhash(td, &map->exit_y,          sizeof map->exit_y         );
          mhash(td, &map->diamonds,        sizeof map->diamonds       );
          mhash(td, &map->diamonds_needed, sizeof map->diamonds_needed);
          mhash(td, &map->diamond_score,   sizeof map->diamond_score  );
          mhash(td, &map->time_score,      sizeof map->time_score     );
          mhash(td, map->data,             map->width * map->height   );
          mhash(td, &map->time,            sizeof map->time           );
          mhash(td, &map->ameba_time,      sizeof map->ameba_time     );
          mhash(td, &map->game_speed,      sizeof map->game_speed     );
          
          mhash_deinit(td, hash);

          len = mhash_get_block_size(MHASH_MD5);
          rv = malloc(len * 2 + 1);
          assert(rv != NULL);
          if(rv != NULL)
            for(int i = 0; i < len; i++)
              sprintf(rv + i * 2, "%.2x", hash[i]);
        }
      free(hash);
    }

  return rv;
}
#else
char * map_calculate_hash(struct map * map DG_UNUSED)
{
  return NULL;
}
#endif
